package com.open.party.cloud.view

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.examine.examineForGanbu.ExamineTypeForGanBuMainActivity
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.view.home.article.fragment.ArticleListFragment
import com.open.party.cloud.view.home.juBao.JuBao12380MainAreaActivity
import com.open.party.cloud.view.login.LoginActivity
import com.open.party.cloud.viewModel.ArticleViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import kotlinx.android.synthetic.main.gan_bu_main_fragment.*


class GanBuMainFragment : Fragment(), OnSuperListener<ArticleEntity> {

    private var viewModel: ArticleViewModel? = null
    private var currView: View? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.gan_bu_main_fragment, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        viewModel = ArticleViewModel()
        initView()
    }

    private fun initView() {
        initTabView()

        initTopItem()
    }

    private fun initTopItem() {
        item01RootView.setOnClickListener {
            XUtils.intent().openActivity(activity, JuBao12380MainAreaActivity::class.java).startInFragment(this)
        }
        item02RootView.setOnClickListener {
            if (!LocalCache.getAutoLogin()) {
                XUtils.intent().openActivity(activity, LoginActivity::class.java)
                    .startInFragment(this)
                return@setOnClickListener
            }
            XUtils.intent().openActivity(activity, ExamineTypeForGanBuMainActivity::class.java).startInFragment(this)

        }
    }

    private fun initTabView() {
        val titleList = ArrayList<String>()
        titleList.add("政策法规")
        titleList.add("任免公示")
        titleList.add("干部管理")
        titleList.add("公务员管理")

        val typeList = ArrayList<String>()
        typeList.add("GBGZ_ZCFG")
        typeList.add("GBGZ_RQGS")
        typeList.add("GBGZ_GZLC")
        typeList.add("RCGZ_GWYGL")
        initTab(
            titleList,
            getFragments(typeList)
        )
    }

    private fun getFragments(typeList: java.util.ArrayList<String>): List<Fragment> {
        val fragments: ArrayList<Fragment> = ArrayList()

        for (type in typeList) {
            val fragment = ArticleListFragment()
            val bundle = Bundle()
            bundle.putString("type", type)
            fragment.arguments = bundle
            fragments.add(fragment)

            Log.e("type", type)
        }

        return fragments
    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(childFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        //        pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles, 0, titles.size <= 4)
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }

    override fun onClick(position: Int, itemData: ArticleEntity, flag: String) {
        XUtils.intent().openActivity(activity, ArticleDetailsActivity::class.java)
            .putStringExtra("id", itemData.id)
            .startInFragment(this)
    }
}